package com.tianfeng.fenghuotoutiao.ui.activity.AppListActibity;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tianfeng.fenghuotoutiao.R;

import java.util.List;



/**
 * Created by DELL on 2018/3/7.
 */

public class AppListAdater extends RecyclerView.Adapter<AppListAdater.ViewHolder> {
    private Context mContext;
    private List<AppListModel.DataBean> data;
    LayoutInflater layoutInflater;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public AppListAdater(Context mContext, List<AppListModel.DataBean> data) {
        this.mContext = mContext;
        this.data = data;
        layoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_applist, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, mOnItemClickListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Glide.with(mContext).load(data.get(position).getAppphoto()).into(holder.iv_appphoto);
        holder.tv_appname.setText(data.get(position).getAppname());
        holder.tv_appdesc.setText(data.get(position).getAppdesc());
        holder.tv_money.setText("奖励RMB"+data.get(position).getAppmoney());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private OnRecyclerViewItemClickListener mOnItemClickListener = null;
     ImageView iv_appphoto;
     TextView tv_appdesc;
     TextView tv_appname;
     TextView tv_money;

        public ViewHolder(View itemView, OnRecyclerViewItemClickListener mListener) {
            super(itemView);
            this.mOnItemClickListener = mListener;
            itemView.setOnClickListener(this);
            iv_appphoto=itemView.findViewById(R.id.iv_appphoto);
            tv_appdesc=itemView.findViewById(R.id.tv_appdesc);
            tv_appname=itemView.findViewById(R.id.tv_appname);
            tv_money=itemView.findViewById(R.id.tv_money);
        }

        @Override
        public void onClick(View v) {
            mOnItemClickListener.onItemClick(v, getAdapterPosition());
        }
    }

    //define interface
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int data);

    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
